package com.qdxwx.web.controller.user;


import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.account.UserAccountService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.course.CourseStudyHistoryService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.data.service.user.UserLetterService;
import com.qdxwx.models.account.UserAccount;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.course.CourseStudyHistory;
import com.qdxwx.models.enums.CourseType;
import com.qdxwx.models.enums.LetterStatus;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserLetter;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private CourseService courseService;
	@Autowired
	private UserAccountService userAccountService;
	@Autowired
	private CourseStudyHistoryService courseStudyHistoryService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private UserLetterService userLetterService;

	/**
	 * 进入个人中心
	 */
	@RequestMapping("/index.html")
	public String ucIndex(HttpServletRequest request, ModelMap model) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return toLoginPage();
		}
		long userId = user.getUserId();

		CourseType courseType = CourseType.get(request.getParameter("courseType"), CourseType.COURSE);
		int isOverdue = TKConvert.toInteger(request.getParameter("isOverdue"));

		PageRequest page = new PageRequest();
		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		page.setCurrentPage(currentPage);
		page.setPageSize(6);
		//获取登录用户ID
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setUserId(userId);
		courseQuery.setSellType(courseType.toString());
		courseQuery.setIsOverdue(isOverdue);

		int total = courseService.countMyCourse(courseQuery);

		List<CourseDTO> courseList = courseService.getMyCourseList(courseQuery, page);

		if (courseList != null && courseList.size() > 0) {
			/*课程学习进度*/
			courseStudyHistoryService.setStudyPercent(courseList, userId);
			/*查询课程最近的一次 学习记录*/
			for (CourseDTO course : courseList) {
				course.setCourseStudyHistory(courseStudyHistoryService.getLastByUserId(userId, course.getCourseId()));
			}
		}

		/*查询用户的最近一条学习记录*/
		CourseStudyHistory lastStudyHistory = courseStudyHistoryService.getLastByUserId(userId);
		if (ObjectUtils.isNotNull(lastStudyHistory)) {
			/*查询最近学习的课程信息*/
			CourseDTO lastStudyCourse = courseService.getDTO(lastStudyHistory.getCourseId());
			if (lastStudyCourse == null) {
				logger.error("course not find " + lastStudyHistory);
			} else {
				lastStudyCourse.setCourseStudyHistory(lastStudyHistory);
				List<Teacher> teachers = teacherService.getListByCourseId(lastStudyHistory.getCourseId());
				if (teachers == null) {
					teachers = new ArrayList<>();
				}
				lastStudyCourse.setTeacherList(teachers);
				/*set学习进度*/
				courseStudyHistoryService.setStudyPercent(lastStudyCourse, userId);
				model.put("lastStudyCourse", lastStudyCourse);
			}
		}
		UserLetter userLetter = new UserLetter();
		userLetter.setIsRead(LetterStatus.UNREAD.ordinal());
		userLetter.setUserId(user.getUserId());
		user.setLetterCount(userLetterService.count(userLetter));
		model.put("totalPages", page.getTotalPages(total));
		model.put("courseType", courseType.toString());
		model.put("isOverdue", isOverdue);
		model.put("courseList", courseList);
		model.put("page", page);
		model.put("user", user);
		model.put("userMobile", TKString.formatMobile(user.getMobile()));
		UserAccount userAccount = userAccountService.getByUserId(userId);
		model.put("userAccount", userAccount);
		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("user/user-index");
		}
		return getViewPath("ucenter/uc-index");
	}
}